/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","comm/datatable/datatable","bch/app/patientManager/nearFollowupUser.ui"],
    function($,$bb,$datatable,$ui){
    var servers = {};

    var URL = $bb.baseUrl("followapp");
    
    /* 请求队列queue */
    servers.ajaxQueue = function(callback){
        $bb.ajax({
            url: URL + "ctrl/projectManage/qryProjectQueue",
            success: function(data){
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "请求队列出错！");
                }
            }
        });
    }



    /**
     * 处理随访提醒参数
     */
    var getParamsFollowupWarn = function(data){
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var param = {
            followQueueId: data.followQueueId,
            patientId: data.patientId,
            taskId: data.taskId,
        };
        var informInfo = {
            followQueueId: data.followQueueId, 
            informType: "2",
            patientId: data.patientId,
            ptSchemeId: data.patientSchemeId, 
            taskId: data.taskId, 
            taskName: data.taskName,
        };  
        var oprInfo = {
            opCode: "1015",
            opNote: "【"+doctorName + "】医生给【"+ data.patientName +"】患者发送的" + data.taskName + "提醒",
            opType: "2",
        };  
        var param2 = {};
        param2.informInfo = informInfo;
        param2.oprInfo = oprInfo;
        return {
            param: param,
            param2: param2,
        };
    }
    /**
     * 发送随访提醒：排班计划及门诊地址列表
     */
    var ajaxWarnSend = function(param,className){
        $bb.ajax({
            url: URL + "followup/service/patient/inform/ptWarnInformDeal",
            data: JSON.stringify({
                data: param
            }),
            success: function (data) {
                if(data.success){
                    $bb.toastShow("发送成功");
                }else{
                    $bb.toastShow(data.msg ? data.msg : "发送失败");
                }
                if(className)$("." + className).modal('hide');
            }
        });
    }
    /**
     * 点击“发送提醒”后的事件函数
     */
    var onWarnSendSelect = function(){
        var className = $(this).data("className");
        $("." + className).modal('hide');
        $bb.toastShow("暂时未开发发送消息功能！");
        
        /*var param = $(this).data("param");
        $.extend(param.informInfo,{
            orderAddress: $("#orderAddress").val(),
            orderDate: $("#orderDate").val(), 
        });
        ajaxWarnSend(param,className);*/
    }
    /**
     * 请求随访任务提醒列表后的回调函数
     */
    var ajaxReminderScheduleCallBack = function(data,param){
        var className = "followupwarnModal";
        $bb.modal({
            "className": className,
            "tit": "随访任务提醒",
        },function(){
            $ui.followupWarnInit({
                dom: $("." + className + " .modal-body"),
                list: data,
            });
            $("." + className).modal('show');
            $("#followupWarnSend").unbind("click");
            $("#followupWarnSend").data({"param":param,"className":className});
            $("#followupWarnSend").click(onWarnSendSelect);
        });  
    }
    /**
     * 请求查询随访提醒的排班、随访日期、随访地址及通知方式（短信、微信或邮件）等
     */
    var ajaxReminderSchedule = function(params,callBack){
        $bb.ajax({
            url: URL + "ctrl/processManage/qryReminderSchedule",
            data: JSON.stringify({
                data: {}
            }),
            success: function (data) {
                if(data.success){
                    callBack(data.data,params.param2);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "查询出错！");
                }
            }
        });
    }

    /******************** 以下是datatable ********************/

    /**
     * 入参
     */
    var inParams = function(args){
        var ageFrom, ageTo;
        if($("#ageFrom-year").val() !="" || $("#ageFrom-month").val() !=""){
            ageFrom = parseInt($("#ageFrom-year").val()*12) + parseInt($("#ageFrom-month").val()*1);
        }else{
            ageFrom = "";
        }
        if($("#ageTo-year").val() !="" || $("#ageTo-month").val() !=""){
            ageTo = parseInt($("#ageTo-year").val()*12) + parseInt($("#ageTo-month").val()*1);
        }else{
            ageTo = "";
        }
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "hisNo": $.trim($("#hisNo").val()),
                "userName": $.trim($("#userName").val()),
                "phoneNo": $.trim($("#phoneNo").val()),
                "queueId": $("#queue").val(),
                "ageFrom": ageFrom,
                "ageTo": ageTo,
                "beginDate": $("#timeFrom").val(),
                "endDate": $("#timeTo").val(),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo ? e.hisNo : e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : "-";
    }
    var columnsGender = function(e){
        return e.gender == 'F' ? '女' : '男';
    }
    var columnsData = function(){
        var columnsArr = [
            {"data":columnsHisNo},
            {"data":'userName'},
            {"data":columnsGender},
            {"data":'phoneNo'},
            {"data":'age'},
            {"data":'pregnancyWeek'},
            {"data":'endTime'},
            {"data":'taskName'},
            {"data":'finishNodeNum'},
            {"data": 'lostNum'},
            {"data": 'queueName'},
            /*{
                "data": null,
                "className":"operate"
            }*/
        ];
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        // 操作
        var h = '<div class="operate-box"><i class="fa fa-bell color-blue" data-toggle="tooltip" data-placement="left" title="提醒"><i></div>';
        $('td.operate', row).html(h);
        $("td.operate .color-blue",row).unbind("click");
        $("td.operate .color-blue",row).click(function(){
            // 注意：此后需要重新写
            var params = getParamsFollowupWarn(data);
            ajaxReminderSchedule(params,ajaxReminderScheduleCallBack);
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        // 搜索 
        $("#serchBtn").click(function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数 
        });
        // 初始化所有工具提示
        $(function(){$("[data-toggle='tooltip']").tooltip();});
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "ctrl/ptmanage/qryWarnUserList?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            //"rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

    return servers;
});